// components/plan/plan.js
const util=require("../../utils/util")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    cardOnsale: {
      type: Object,
      value: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgPath: "/images",
    agreeValue: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    kaiTong() {
      wx.showToast({
        title: '用成长卡开通',
      })
    },
    radioChange(e) {
      // console.log("radioChange,e", e)
      this.setData({
        agreeValue: e.detail.value
      })
      // console.log("agreeValue", this.data.agreeValue)
    },
    onGree(event) {
      console.log("onGree,event", event)
    },
    /**
     * 立即开通
     */
    liZhiKaiTong() {

      if (this.data.agreeValue != 1) {
        // wx.showToast({
        //   title: '请先同意《魔杰龙计划章程》和《隐私政策》',
        // })
        wx.showModal({
          title: '提示',
          content: '请先同意《魔杰龙计划章程》和《隐私政策》',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              // console.log('用户点击确定')
              return;
            }
          }
        })

        return;
      }

      // wx.showToast({
      //   title: '立即开通',
      // })

      const api = require("../../api/api")
      api.order.transactions({
        goods_info: [{
          goods_id: this.data.cardOnsale.id,
          buy_number: 1,
          coupon_id:"",
          channel:util.getChannel(),
        }],
        price: this.data.cardOnsale.price,
        success(res) {
          // console.log("transactions.res", res)
          let order_sn=res.data.data.order_sn;
          api.order.payment({
            order_sn:order_sn,
            pay_type:'wx',
            pay_client:'lite',
            success(resPayment){
              // console.log("payment,resPayment",resPayment)

              let dataPayment=resPayment.data.data;

              //微信支付
              wx.requestPayment({
                timeStamp: dataPayment.timeStamp,
                nonceStr: dataPayment.nonceStr,
                package: dataPayment.package,
                signType: 'MD5',
                paySign: dataPayment.paySign,
                success (resPay) { 
                  console.log("wx.requestPayment,resPay",resPay)

                  wx.showToast({
                    title: '开通成功',
                  })

                  wx.navigateTo({
                    url: '/pages/service/index',
                  })
                },
                fail (resPay) {
                  console.log("wx.requestPayment,resPay",resPay)
                 }
              })
            }
          })
        }
      })

      wx.navigateTo({
        url: '/pages/orderSheet/paying',
      })
    },
    /**
     * 计划章程
     */
    showJiHua() {
      wx.showModal({
        title: '提示',
        content: '这是魔杰龙计划章程',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          }
        }
      })
    },
    /**
     * 隐私政策
     */
    showYinSi() {
      wx.showModal({
        title: '提示',
        content: '这是隐私政策',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          }
        }
      })
    }
  },
  lifetimes: {
    attached() {
      // console.log("plan attached")
      wx.setNavigationBarTitle({
        title: '加入计划',
      })

      const util = require("../../utils/util")
      this.setData({
        imgPath: util.getImgPath()
      })
    }
  }
})